package com.zhongfl.crm.lixiao.batch.task;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhongfl.crm.lixiao.batch.PagingTaskService;
import com.zhongfl.crm.lixiao.batch.service.OrderRevisitLogSyncFromBoss2LXService;
import com.zhongfl.crm.lixiao.mapper.OrderMemorandumTaskMapper;
import com.zhongfl.crm.model.boss.OrderMemorandum;
import com.zhongfl.crm.service.boss.LXRevisitLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import tech.heptagram.core.common.model.PageVo;

import javax.annotation.Resource;
import java.util.List;


/**
 * BOSS跟进->励销 任务
 *
 * @author wang.yuchuan
 * @date 2018/6/29
 */
@Component
@Slf4j
public class OrderRevisitLogSyncFromBoss2LXTask extends PagingTaskService<OrderMemorandum> {

    @Resource
    private OrderMemorandumTaskMapper orderMemorandumTaskMapper;

    @Resource
    private OrderRevisitLogSyncFromBoss2LXService orderRevisitLogSyncFromBoss2LXService;

    @Resource
    private LXRevisitLogService lxRevisitLogService;


    @Override
    public boolean lock(OrderMemorandum task) {
        log.info("lock 跟进记录同步: {}", task.getId());
        return orderMemorandumTaskMapper.lock(task.getId()) == DEFAULT_UPDATE_COUNT;
    }

    @Override
    public void exec(OrderMemorandum task) throws RuntimeException {
        log.info("开始同步跟进记录: {}", task.getId());
        orderRevisitLogSyncFromBoss2LXService.sync(task);
        log.info("完成同步跟进记录: {}", task.getId());
    }

    @Override
    public void success(OrderMemorandum task) throws RuntimeException {
        log.info("success 跟进记录同步: {}", task.getId());
        orderMemorandumTaskMapper.success(task);
    }

    @Override
    public void fail(OrderMemorandum task, String failedMsg) throws RuntimeException {
        log.info("fail 跟进记录同步: {}", task.getId());
        orderMemorandumTaskMapper.fail(task);
    }

    @Override
    public void reset(OrderMemorandum task) throws RuntimeException {
        log.info("reset 跟进记录同步: {}", task.getId());
        orderMemorandumTaskMapper.reset(task);
    }

    @Override
    public int getTaskTimes(OrderMemorandum task) {
        return task.getRetryTimes();
    }

    @Override
    public void setTaskTimes(OrderMemorandum task, Integer taskTimes) {
        task.setRetryTimes(taskTimes);
    }

    @Override
    public PageInfo<OrderMemorandum> find(PageVo pageVo) {

        PageHelper.startPage(pageVo.getPage(), pageVo.getPageSize());

        List<OrderMemorandum> orderMemorandums = orderMemorandumTaskMapper.selectUnDealedTasks(pageVo);
        orderMemorandums.forEach(item-> lxRevisitLogService.setOrderAttachmentList(item));

        return new PageInfo<>(orderMemorandums);
    }
}
